package com.victor.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.victor.entity.FreeTime;
import com.victor.entity.Picturebook;
import com.victor.entity.Recharge;

@Mapper
public interface FreetimeDao {
	/**
	 * 通过用户id和书id查询freetime表
	 * 
	 * @param userid
	 * @param picid
	 * @return
	 */
	@Select({ "SELECT * FROM freetime WHERE userid=#{userid} AND picid=#{picid}" })
	FreeTime getFreeByUseridAndPicid(@Param("userid") String userid, @Param("picid") String picid);

	/**
	 * 将freetime信息插入数据库
	 * 
	 * @param free
	 * @return
	 */
	@Insert({ "INSERT INTO freetime (userid,picid,starttime,endtime,state) VALUES (#{userid},#{picid},#{starttime},#{endtime},#{state}) " })
	int addFreeTime(FreeTime free);

	/**
	 * 查出所有状态为0的数据
	 * 
	 * @return
	 */
	@Select({ "SELECT * FROM freetime WHERE state='0'" })
	List<FreeTime> getFreeDoing();

	/**
	 * 已经过期,修改状态
	 * 
	 * @param freetimeid
	 * @return
	 */
	@Update({ "update freetime set state='1' where id=#{freetimeid}" })
	int updateState(@Param("freetimeid") Integer freetimeid);

	/**
	 * 购买后将状态改为2
	 * 
	 * @param freetimeid
	 * @return
	 */
	@Update({ "update freetime set state='2' where id=#{freetimeid}" })
	int updateStateTo2(@Param("freetimeid") Integer freetimeid);

	/**
	 * 通过回调订单号查询订单信息
	 * 
	 * @param orderid
	 * @return
	 */
	@Select({ "select * from recharge where ordernumber=#{orderid}" })
	Recharge getRechargeByOrderid(@Param("orderid") String orderid);

	/**
	 * 通过系列id查询绘本list
	 * 
	 * @param sid
	 * @return
	 */
	List<Picturebook> getPicturebooksBySid(@Param("sid")String sid);

}
